﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TD.TheGame
{
    public class Castle : DrawableGameComponent
    {
        public int MaxHealth
        {
            get
            {
                int health = 0;
                foreach (var castlePart in castleParts)
                {
                    if (castlePart.Active)
                    {
                        health += castlePart.MaxHealth;
                    }
                }
                return health;
            }
        }

        //Health
        public int Health
        {
            get 
            {
                int health = 0;
                foreach (var castlePart in castleParts)
                {
                    if (castlePart.Active)
                    {
                        health += castlePart.Health;
                    }
                }
                return health;
            }
        }

        public CastleShooterArrow[] Arrows
        {
            get
            {
                return arrows;
            }
        }

        private CastleSprite castleSideLeft;
        private CastleSprite castleSideRight;
        private CastleSprite castleMid;
        private CastleSprite castleMidTop;
        private CastleShooterArrow[] arrows;

        //Fences
        private CastleFence pastFence;
        private CastleFence presentFence;
        private CastleFence futureFence;
        public CastleFence currentFence;

        public List<CastleSprite> castleParts = new List<CastleSprite>();
        public Castle(Game game) : base(game)
        {
            castleSideLeft = new CastleSideTower(game, new Vector2(0, 456), false, 400, SpriteEnumState.Past, SpriteEnumState.Left);
            castleSideRight = new CastleSideTower(game, new Vector2(251, 456), false, 400, SpriteEnumState.Past,SpriteEnumState.Right);
            castleMid = new CastleMid(game, new Vector2(113,607), true, 400, SpriteEnumState.Past);
            castleMidTop = new CastleMidTop(game, new Vector2(147.5f,473), false, 400,SpriteEnumState.Past);
            //castleFence = new CastleFence(game, new Vector2(389,600), true, 607, SpriteEnumState.Past);
            pastFence = new PastFence(game, new Vector2(389, 600), false, 400, SpriteEnumState.Normal);
            presentFence = new PresentFence(game, new Vector2(389, 600), false, 400, SpriteEnumState.Normal);
            futureFence = new FutureFence(game, new Vector2(389, 600), false, 400, SpriteEnumState.Normal);
            currentFence = pastFence;
            //castleSideLeft.position = new Vector2(0,game.Window.ClientBounds.Height - castleSideLeft.BoundingRectangle.Height);
            //castleMid.position = new Vector2(castleSideLeft.BoundingRectangle.Width, game.Window.ClientBounds.Height - castleMid.BoundingRectangle.Height);
            //castleMidTop.position = new Vector2(castleMid.position.X + castleMid.BoundingRectangle.Width * 0.25f, game.Window.ClientBounds.Height - castleMid.BoundingRectangle.Height - castleMidTop.BoundingRectangle.Height);
            //castleSideRight.position = new Vector2(castleMid.position.X + castleMid.BoundingRectangle.Width, game.Window.ClientBounds.Height - castleSideRight.BoundingRectangle.Height);
            //UpdateCastleFence(SpriteEnumState.Future, 500, 40);

            //Add castleParts to list
            castleParts.Add(castleSideLeft);
            castleParts.Add(castleSideRight);
            castleParts.Add(castleMid);
            castleParts.Add(castleMidTop);

            //Add Arrows in new Array
            arrows = new CastleShooterArrow[castleSideLeft.Arrows.Length + castleSideRight.Arrows.Length];
            Array.Copy(castleSideLeft.Arrows, arrows, castleSideLeft.Arrows.Length);
            //Array.Copy(castleSideRight.Arrows, arrows, castleSideLeft.Arrows.Length, castleSideRight.Arrows.Length);
            Array.Copy(castleSideRight.Arrows, 0, arrows, castleSideLeft.Arrows.Length, castleSideRight.Arrows.Length);
        }

        public override void Initialize()
        {
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            foreach (var castlePart in castleParts)
            {
                if (castlePart.Active)
                {
                    castlePart.Update(gameTime);
                }
            }
            if (currentFence.Active)
            {
                currentFence.Update(gameTime);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (var castlePart in castleParts)
            {
                if (castlePart.Active)
                {
                    castlePart.Draw(gameTime);
                }
            }
            if (currentFence.Active)
            {
                currentFence.Draw(gameTime);
            }
            base.Draw(gameTime);
        }

        public void UpdateCastleFence(SpriteEnumState state, int health, int damage)
        {
            if (state == SpriteEnumState.Past)
            {
                currentFence = pastFence;
                currentFence.Damage = damage;
                currentFence.Health = health;
                currentFence.MaxHealth = health;
                currentFence.Active = true;
            }
            if (state == SpriteEnumState.Normal)
            {
                currentFence = presentFence;
                currentFence.Damage = damage;
                currentFence.Health = health;
                currentFence.MaxHealth = health;
                currentFence.Active = true;
            }
            if (state == SpriteEnumState.Future)
            {
                currentFence = futureFence;
                currentFence.Damage = damage;
                currentFence.Health = health;
                currentFence.MaxHealth = health;
                currentFence.Active = true;
            }
        }


        public void UpgradeLeftSideTower(SpriteEnumState state)
        {
            if (castleSideLeft.Active == true)
            {
                castleSideLeft.Upgrade(state);   
            }
            else
            {
                Console.WriteLine("Castle isnt actived yet");
            }
        }

        public void UpgradeRightSideTower(SpriteEnumState state)
        {
            if (castleSideRight.Active == true)
            {
                castleSideRight.Upgrade(state);   
            }
            else
            {
                Console.WriteLine("Castle isnt actived yet");
            }
        }

        public void EnableLeftSideTower()
        {
            castleSideLeft.Active = true;
        }

        public void EnableRightSideTower()
        {
            castleSideRight.Active = true;
        }

        public void EnableMidTopTower()
        {
            castleMidTop.Active = true;
        }
    }
}
